import is, { hasProperty } from '@form-create/utils/lib/type'

export function makeRequiredRule () {
	return {
		type: 'Required', field: 'formCreateEffect>required', title: '是否必填'
	}
}

export function makeOptionsRule (to, flag) {
	const options = [
		{ 'label': 'JSON数据', 'value': 0 },
		{ 'label': '接口数据', 'value': 1 }
	]

	const control = [
		{
			value: 0,
			rule: [
				{
					type: 'Struct',
					value: [],
					field: 'formCreate' + upper(to).replace('.', '>'),
					props: {}
				}
			]
		},
		{
			value: 1,
			rule: [
				{
					type: 'Fetch',
					field: 'formCreateEffect>fetch',
					props: {
						to
					}
				}
			]
		}
	]

	if (flag !== false) {
		options.splice(0, 0, { 'label': '静态数据', 'value': 2 })
		control.push({
			value: 2,
			rule: [
				{
					type: 'TableOptions',
					field: 'formCreate' + upper(to).replace('.', '>'),
					value: [],
					props: {},
					style: 'width:100%'
				}
			]
		})
	}

	return {
		type: 'radio',
		title: '选项数据',
		field: 'optionType',
		value: flag !== false ? 2 : 0,
		options,
		props: {
			type: 'button'
		},
		control
	}
}

export function upper (str) {
	return str.replace(str[0], str[0].toLocaleUpperCase())
}



